| Conditions | 1 |
| Paths | 1 |
| Total Lines | 191 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const GitAmp = (function(exports, $) { |
||
| 132 | const Gui = (function() { |
||
| 133 | const scaleFactor = 6; |
||
| 134 | const textColor = '#ffffff'; |
||
| 135 | const maxLife = 20000; |
||
| 136 | |||
| 137 | function Event(event, svg) { |
||
| 138 | this.event = event; |
||
| 139 | this.svg = svg; |
||
| 140 | } |
||
| 141 | |||
| 142 | Event.prototype.getSize = function() { |
||
| 143 | return Math.max(Math.sqrt(Math.abs(this.event.getMessage().length)) * scaleFactor, 3); |
||
| 144 | }; |
||
| 145 | |||
| 146 | Event.prototype.getText = function() { |
||
| 147 | switch(this.event.getType()){ |
||
| 148 | case 'PushEvent': |
||
| 149 | return this.event.getActorName() + ' pushed to ' + this.event.getRepositoryName(); |
||
| 150 | case 'PullRequestEvent': |
||
| 151 | return this.event.getActorName() + ' ' + this.event.getAction() + ' ' + ' a PR for ' + this.event.getRepositoryName(); |
||
| 152 | case 'IssuesEvent': |
||
| 153 | return this.event.getActorName() + ' ' + this.event.getAction() + ' an issue in ' + this.event.getRepositoryName(); |
||
| 154 | case 'IssueCommentEvent': |
||
| 155 | return this.event.getActorName() + ' commented in ' + this.event.getRepositoryName(); |
||
| 156 | case 'ForkEvent': |
||
| 157 | return this.event.getActorName() + ' forked ' + this.event.getRepositoryName(); |
||
| 158 | case 'CreateEvent': |
||
| 159 | return this.event.getActorName() + ' created ' + this.event.getRepositoryName(); |
||
| 160 | case 'WatchEvent': |
||
| 161 | return this.event.getActorName() + ' watched ' + this.event.getRepositoryName(); |
||
| 162 | } |
||
| 163 | }; |
||
| 164 | |||
| 165 | Event.prototype.getBackgroundColor = function() { |
||
| 166 | switch(this.event.getType()){ |
||
| 167 | case 'PushEvent': |
||
| 168 | return '#22B65D'; |
||
| 169 | case 'PullRequestEvent': |
||
| 170 | return '#8F19BB'; |
||
| 171 | case 'IssuesEvent': |
||
| 172 | return '#ADD913'; |
||
| 173 | case 'IssueCommentEvent': |
||
| 174 | return '#FF4901'; |
||
| 175 | case 'ForkEvent': |
||
| 176 | return '#0184FF'; |
||
| 177 | case 'CreateEvent': |
||
| 178 | return '#00C0C0'; |
||
| 179 | case 'WatchEvent': |
||
| 180 | return '#E60062'; |
||
| 181 | } |
||
| 182 | }; |
||
| 183 | |||
| 184 | Event.prototype.getRingAnimationDuration = function() { |
||
| 185 | if (this.event.getType() === 'PullRequestEvent') { |
||
| 186 | return 10000; |
||
| 187 | } |
||
| 188 | |||
| 189 | return 3000; |
||
| 190 | }; |
||
| 191 | |||
| 192 | Event.prototype.getRingRadius = function() { |
||
| 193 | if (this.event.getType() === 'PullRequestEvent') { |
||
| 194 | return 600; |
||
| 195 | } |
||
| 196 | |||
| 197 | return 80; |
||
| 198 | }; |
||
| 199 | |||
| 200 | Event.prototype.draw = function(width, height) { |
||
| 201 | let no_label = false; |
||
| 202 | let size = this.getSize(); |
||
| 203 | |||
| 204 | const self = this; |
||
| 205 | |||
| 206 | //noinspection JSUnresolvedFunction |
||
| 207 | Math.seedrandom(this.event.getMessage()); |
||
| 208 | let x = Math.random() * (width - size) + size; |
||
| 209 | let y = Math.random() * (height - size) + size; |
||
| 210 | |||
| 211 | let circle_group = this.svg.append('g') |
||
| 212 | .attr('transform', 'translate(' + x + ', ' + y + ')') |
||
| 213 | .attr('fill', this.getBackgroundColor()) |
||
| 214 | .style('opacity', 1); |
||
| 215 | |||
| 216 | let ring = circle_group.append('circle'); |
||
| 217 | ring.attr({r: size, stroke: 'none'}); |
||
| 218 | ring.transition() |
||
| 219 | .attr('r', size + this.getRingRadius()) |
||
| 220 | .style('opacity', 0) |
||
| 221 | .ease(Math.sqrt) |
||
| 222 | .duration(this.getRingAnimationDuration()) |
||
| 223 | .remove(); |
||
| 224 | |||
| 225 | let circle_container = circle_group.append('a'); |
||
| 226 | circle_container.attr('xlink:href', this.event.getUrl()); |
||
| 227 | circle_container.attr('target', '_blank'); |
||
| 228 | circle_container.attr('fill', textColor); |
||
| 229 | |||
| 230 | let circle = circle_container.append('circle'); |
||
| 231 | circle.classed(this.event.getType(), true); |
||
| 232 | circle.attr('r', size) |
||
| 233 | .attr('fill', this.getBackgroundColor()) |
||
| 234 | .transition() |
||
| 235 | .duration(maxLife) |
||
| 236 | .style('opacity', 0) |
||
| 237 | .remove(); |
||
| 238 | |||
| 239 | circle_container.on('mouseover', function() { |
||
| 240 | circle_container.append('text') |
||
| 241 | .text(self.getText()) |
||
| 242 | .classed('label', true) |
||
| 243 | .attr('text-anchor', 'middle') |
||
| 244 | .attr('font-size', '0.8em') |
||
| 245 | .transition() |
||
| 246 | .delay(1000) |
||
| 247 | .style('opacity', 0) |
||
| 248 | .duration(2000) |
||
| 249 | .each(function() { no_label = true; }) |
||
| 250 | .remove(); |
||
| 251 | }); |
||
| 252 | |||
| 253 | circle_container.append('text') |
||
| 254 | .text(this.getText()) |
||
| 255 | .classed('article-label', true) |
||
| 256 | .attr('text-anchor', 'middle') |
||
| 257 | .attr('font-size', '0.8em') |
||
| 258 | .transition() |
||
| 259 | .delay(2000) |
||
| 260 | .style('opacity', 0) |
||
| 261 | .duration(5000) |
||
| 262 | .each(function() { no_label = true; }) |
||
| 263 | .remove(); |
||
| 264 | }; |
||
| 265 | |||
| 266 | function Gui() { |
||
| 267 | //noinspection JSUnresolvedVariable |
||
| 268 | this.svg = exports.d3.select('#area').append('svg'); |
||
| 269 | |||
| 270 | exports.addEventListener('resize', this.resize.bind(this)); |
||
| 271 | |||
| 272 | this.setupVolumeSlider(); |
||
| 273 | this.resize(); |
||
| 274 | } |
||
| 275 | |||
| 276 | Gui.prototype.setupVolumeSlider = function() { |
||
| 277 | //noinspection JSUnresolvedFunction |
||
| 278 | $('#volumeSlider').slider({ |
||
| 279 | max: 100, |
||
| 280 | min: 0, |
||
| 281 | value: volume * 100, |
||
| 282 | slide: function (event, ui) { |
||
| 283 | //noinspection JSUnresolvedVariable |
||
| 284 | exports.Howler.volume(ui.value/100.0); |
||
| 285 | }, |
||
| 286 | change: function (event, ui) { |
||
| 287 | //noinspection JSUnresolvedVariable |
||
| 288 | exports.Howler.volume(ui.value/100.0); |
||
| 289 | } |
||
| 290 | }); |
||
| 291 | }; |
||
| 292 | |||
| 293 | Gui.prototype.getWidth = function() { |
||
| 294 | return exports.innerWidth; |
||
| 295 | }; |
||
| 296 | |||
| 297 | Gui.prototype.getHeight = function() { |
||
| 298 | return exports.innerHeight - $('header').height(); |
||
| 299 | }; |
||
| 300 | |||
| 301 | Gui.prototype.resize = function() { |
||
| 302 | this.svg.attr('width', this.getWidth()); |
||
| 303 | this.svg.attr('height', this.getHeight()); |
||
| 304 | }; |
||
| 305 | |||
| 306 | Gui.prototype.drawEvent = function(event) { |
||
| 307 | if (document.hidden) { |
||
| 308 | return; |
||
| 309 | } |
||
| 310 | |||
| 311 | new Event(event, this.svg).draw(this.getWidth(), this.getHeight()); |
||
| 312 | |||
| 313 | // Remove HTML of decayed events |
||
| 314 | // Keep it less than 50 |
||
| 315 | let $area = $('#area'); |
||
| 316 | if($area.find('svg g').length > 50){ |
||
| 317 | $area.find('svg g:lt(10)').remove(); |
||
| 318 | } |
||
| 319 | }; |
||
| 320 | |||
| 321 | return Gui; |
||
| 322 | }()); |
||
| 323 | |||
| 575 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.